﻿#include "stdafx.h"
#include "main_data_c.h"
#include "MySocket.h"

int MySocket::onAsyncRecv()
{
    int count = 0;
    int offset = 0;
    int n = ::recv(mainData.socketDataBuf_54CA6E0.sock, (char*)mainData.socketDataBuf_54CA6E0.recvBuf + mainData.socketDataBuf_54CA6E0.recvLen, 0x2000 - mainData.socketDataBuf_54CA6E0.recvLen, 0);
    if (n < 0) {
        if (WSAGetLastError() == WSAEWOULDBLOCK)
        {
            return 1;
        }
        else
        {
            int err = WSAGetLastError();
            return 1;
        }
    }
    else if (n == 0) {
        return 1;
    }
    else {
        mainData.socketDataBuf_54CA6E0.recvLen += n;
        if (mainData.socketDataBuf_54CA6E0.recvLen < 4) return 3;

        while (mainData.socketDataBuf_54CA6E0.recvLen > 0)
        {
            BYTE c = mainData.socketDataBuf_54CA6E0.recvBuf[offset];
            if (c == 0xC1 || c == 0xC3)
                count = mainData.socketDataBuf_54CA6E0.recvBuf[offset + 1];
            else {
                if (c != 0xC2 && c != 0xC4) {
                    mainData.socketDataBuf_54CA6E0.recvLen = 0;
                    OutputDebugString("数据包协议头错误\n");
                    return 0;
                }
                count = (mainData.socketDataBuf_54CA6E0.recvBuf[offset + 1] << 8) + mainData.socketDataBuf_54CA6E0.recvBuf[offset + 2];
            }
            if (count <= 0) return 0;
            if (count > mainData.socketDataBuf_54CA6E0.recvLen) {
                if (offset > 0) {
                    memmove(mainData.socketDataBuf_54CA6E0.recvBuf, mainData.socketDataBuf_54CA6E0.recvBuf + offset, mainData.socketDataBuf_54CA6E0.recvLen);
                }
                break;
            }
            copyPacket(mainData.socketDataBuf_54CA6E0.recvBuf + offset, count);
            offset += count;
            mainData.socketDataBuf_54CA6E0.recvLen -= count;
        }
        return 0;
    }
    return 0;
}

void MySocket::onAsyncSend()
{
    if (mainData.socketDataBuf_54CA6E0.sendLen <= 0)
        return;

    int offset = 0;
    while (mainData.socketDataBuf_54CA6E0.sendLen > 0)
    {
        int n = ::send(mainData.socketDataBuf_54CA6E0.sock, (char*)mainData.socketDataBuf_54CA6E0.sendBuf + offset, mainData.socketDataBuf_54CA6E0.sendLen, 0);
        if (n <= 0) {
            if (WSAGetLastError() == WSAEWOULDBLOCK) return;
            close();
            return;
        }
        offset += n;
        mainData.socketDataBuf_54CA6E0.sendLen -= n;
    }
}

void MySocket::close()
{
	closesocket(mainData.socketDataBuf_54CA6E0.sock);
	mainData.socketDataBuf_54CA6E0.sock = INVALID_SOCKET;
}

BYTE* MySocket::checkDataBuf()
{
	for (size_t i = 0; i < 300; i++)
	{
		auto& dataItem = mainData.socketDataBuf_54CA6E0.dataItems[i];
		if (dataItem.fHaveData) {
			dataItem.fHaveData = false;
			return dataItem.bytes;
		}
	}
	return NULL;
}

int MySocket::copyPacket(BYTE* data, int dataLen)
{
    if (dataLen > 0x2000)
        return 2;

    for (size_t i = 0; i < 300; i++)
    {
        auto& item = mainData.socketDataBuf_54CA6E0.dataItems[i];
        if (!item.fHaveData) {
            item.fHaveData = TRUE;
            item.dataLen = dataLen;
            memcpy(item.bytes, data, dataLen);
            return 1;
        }
    }
    return 0;
}
